//
//  SHWithdrawCashViewController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/6/2.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "SHPayPasswordView.h"
#import "XLPaymentLoadingHUD.h"
#import "XLPaymentSuccessHUD.h"
#import "SHUserPayPasswordController.h"
#import "SHButton.h"
#import "UserModel.h"
#import "SHTextField.h"
#import "BankCardModel.h"
#import "UserBalanceModel.h"
#import "SHBalanceModelView.h"
#import "SHWithdrawCashBankCardController.h"
#import "SHWithdrawCashViewController.h"

@interface SHWithdrawCashViewController ()<UITextFieldDelegate,SHWithdrawCashBankCardControllerDelegate,SHPayPasswordViewDelegate>
@property(nonatomic,strong)UIView *bankCardBg;
@property(nonatomic,strong)UIView *bgView;//背景
@property(nonatomic,strong)SHLabel *cardIdLabel;//卡号标签
@property(nonatomic,strong)SHLabel *banckCardNameLabel;//银行卡名称
@property(nonatomic,strong)SHLabel *cashMoneyLabel;//提现金额标签
@property(nonatomic,strong)SHLabel *logImg;//¥字符号
@property(nonatomic,strong)SHTextField *moneyTf;//提现文本框
@property(nonatomic,strong)SHLabel *balanceLabel;//余额
@property(nonatomic,strong)SHButton *withdrawCashAllMoney;//全部体现

@property(nonatomic,strong)SHButton *withdrawCashBtn;//提现按钮
@property(nonatomic,strong)BankCardModel *bankCardModel;//提现卡模型
@property(nonatomic,copy)NSString *bankCardCodeId;//卡号
@property(nonatomic,copy)NSString *bankCardName;//银行名称
@property(nonatomic,strong)NSMutableArray *bankCardDataArray;
@property(nonatomic,strong)SHPayPasswordView *payPasswordView;


@property (nonatomic, strong) UIView * grayBg;// 灰色背景



@end

@implementation SHWithdrawCashViewController
//支付密码框
- (SHPayPasswordView *)payPasswordView{
    
    _payPasswordView = [[SHPayPasswordView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    
    _payPasswordView.backgroundColor = [UIColor clearColor];
    _payPasswordView.delegate = self;
    
    return _payPasswordView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KBASEBGCOLOR;
    [self configUI];
    [self requestBankCardInfo];
    
}

#pragma mark - 取消按钮
- (void)createRightItems{
    
    UIBarButtonItem *fixedButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    fixedButton.width = -5;
    
    SHButton *cancelBtn = [SHButton buttonWithType:UIButtonTypeCustom title:@"返回" font:14 titleColor:KSHTEXTCOLOR textAlignment:1 forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(backToVc:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:cancelBtn];
    self.navigationItem.leftBarButtonItems = @[fixedButton,leftItem];
    
    
}

#pragma mark - 初始化UI
- (void)configUI{
    
    if (self.withdrawCashType == SHWithdrawCashD0) {
        self.navigationItem.title = @"转出";
        
    }else if(self.withdrawCashType == SHWithdrawCashMyWallte) {
        self.navigationItem.title = @"提现";
        self.withdrawal_balance = [NSNumber numberWithFloat:[UserModel shareManager].balance];
        
    }else{
        self.navigationItem.title = @"转入到D0";
        self.withdrawal_balance = [NSNumber numberWithFloat:[UserModel shareManager].balance];
        
    }
    if(self.withdrawCashType == SHWithdrawCashTurnOutToD0){
        
        [self bankCardBg];
        [self banckCardNameLabel];
        [self cardIdLabel];
        
        [self bgView];
        
        [self cashMoneyLabel];
        [self logImg];
        [self moneyTf];
        [self balanceLabel];
        [self withdrawCashAllMoney];
        [self withdrawCashBtn];
        self.bankCardBg.hidden = YES;
        self.cashMoneyLabel.text = @"转入金额";
        
        [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).offset(__I6Height(0));
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.width.equalTo(@(KScreenWidth));
            make.height.equalTo(@(__I6Height(130)));
        }];
        
    }else{
        
        [self bankCardBg];
        [self banckCardNameLabel];
        [self cardIdLabel];
        
        [self bgView];
        
        [self grayBg];
        
        [self cashMoneyLabel];
        [self logImg];
        [self moneyTf];
        [self balanceLabel];
        [self withdrawCashAllMoney];
        [self withdrawCashBtn];
    }
}

#pragma mark - 懒加载
- (UIView *)bankCardBg{
    
    if (!_bankCardBg) {
        
        _bankCardBg = [[UIView alloc]init];
        _bankCardBg.backgroundColor = [UIColor whiteColor];
        UITapGestureRecognizer *selectedBankCard = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedBankCardAction:)];
        [_bankCardBg addGestureRecognizer:selectedBankCard];
        [self.view addSubview:_bankCardBg];
        [_bankCardBg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(__I6Height(10));
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.width.equalTo(@(KScreenWidth));
            make.height.equalTo(@(__I6Height(56)));
        }];
        UIImageView *arrowImg = [[UIImageView alloc]init];
        arrowImg.image = [UIImage imageNamed:@"jmarrow@2x.png"];
        [_bankCardBg addSubview:arrowImg];
        [arrowImg  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bankCardBg.mas_centerY);
            make.right.equalTo(self.bankCardBg.mas_right).with.offset(__I6Width(-15));
            make.height.mas_equalTo(__I6Height(14));
            make.width.mas_equalTo(__I6Width(7.5));
        }];
        
        UILabel * choose = [UILabel new];
        choose.text = @"请选择";
        choose.textColor = KSHGRAYTEXTCOLOR;
        choose.font = [UIFont systemFontOfSize:12];
        [_bankCardBg addSubview:choose];
        
        [choose mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bankCardBg);
            make.right.equalTo(arrowImg.mas_left).offset(-10);
            make.top.bottom.equalTo(self.bankCardBg);
        }];
        
    }
    return _bankCardBg;
}

#pragma mark - 选择卡号
- (void)selectedBankCardAction:(UITapGestureRecognizer *)ges{
    
    SHWithdrawCashBankCardController * withdrawCashBankCardVC = [[SHWithdrawCashBankCardController alloc]init];
    
    withdrawCashBankCardVC.delegate = self;
    [self.navigationController pushViewController:withdrawCashBankCardVC animated:YES];
    
}
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).offset(__I6Height(66));
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.width.equalTo(@(KScreenWidth));
            make.height.equalTo(@(__I6Height(140)));
        }];
    }
    return _bgView;
}

- (UIView *)grayBg{
    if (!_grayBg) {
        _grayBg = [UIView new];
        _grayBg.backgroundColor = KBASEBGCOLOR;
        [_bgView addSubview:_grayBg];
        
        [_grayBg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView);
            make.top.equalTo(self.bankCardBg.mas_bottom);
            make.height.mas_offset(__I6Height(10));
        }];
    }
    
    return _grayBg;
}

- (SHLabel *)cardIdLabel{
    if (!_cardIdLabel) {
        
        _cardIdLabel = [SHLabel labelWithText:@"" font:13 textColor:KSHGRAYTEXTCOLOR textAlignment:NSTextAlignmentLeft];
        [_bankCardBg addSubview:_cardIdLabel];
        [_cardIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.banckCardNameLabel.mas_bottom).offset(__I6Height(8));
            make.left.equalTo(self.bankCardBg.mas_left).offset(__I6Width(15));
            make.height.equalTo(@(__I6Height(13)));
        }];
    }
    return _cardIdLabel;
}

- (SHLabel *)banckCardNameLabel{
    
    if (!_banckCardNameLabel) {
        _banckCardNameLabel = [SHLabel labelWithTextFont:15 textColor:KSHTEXTCOLOR textAlignment:0];
        _banckCardNameLabel.text = @"";
        [_bankCardBg addSubview:_banckCardNameLabel];
        [_banckCardNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bankCardBg.mas_left).offset(__I6Width(15));
            make.right.equalTo(self.bankCardBg.mas_right).offset(__I6Width(-30));
            make.top.mas_equalTo(self.bankCardBg.mas_top).with.offset(__I6Height(10));
            make.height.equalTo(@(__I6Height(15)));
        }];
    }
    return _banckCardNameLabel;
}

- (SHLabel *)cashMoneyLabel{
    
    if (!_cashMoneyLabel) {
        _cashMoneyLabel = [SHLabel labelWithText:@"提现金额" font:15 textColor:KSHTEXTCOLOR textAlignment:NSTextAlignmentLeft];
        [_bgView addSubview:_cashMoneyLabel];
        [_cashMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(__I6Height(15)));
            make.left.mas_equalTo(self.bgView.mas_left).with.offset(__I6Width(15));
            make.top.mas_equalTo(self.bgView.mas_top).with.offset(__I6Height(20));
        }];
    }
    
    return _cashMoneyLabel;
}

- (SHLabel *)logImg{
    if (!_logImg) {
        _logImg = [[SHLabel alloc]init];
        //  _logImg.image = [UIImage imageNamed:@"rmb@2x"];
        _logImg.text = @"¥";
        _logImg.font = KSystemFont(__I6Width(25));
        [_bgView addSubview:_logImg];
        [_logImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.cashMoneyLabel.mas_bottom).offset(__I6Height(28));
            make.left.mas_equalTo(self.bgView.mas_left).with.offset(__I6Width(15));
            make.height.equalTo(@(__I6Height(25)));
            make.width.mas_offset(__I6Width(20));
        }];
    }
    
    return _logImg;
}
- (SHTextField *)moneyTf{
    if (!_moneyTf) {
        _moneyTf  = [[[SHTextField  alloc]init] labelText:@"" font:25 textColor:KSHTEXTCOLOR placeholder:@"0.00" placeholderFont:25 placeholderTextColor:KSHTEXTPLACEHOLDERCOLOR secureTextEntry:NO clearButtonMode:UITextFieldViewModeAlways borderStyle:UITextBorderStyleNone keyboardType:UIKeyboardTypeDecimalPad];
        
        _moneyTf.delegate = self;
        _moneyTf.autocorrectionType = UITextAutocorrectionTypeNo;
        [_bgView addSubview:_moneyTf];
        [_moneyTf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(__I6Height(25));
            make.left.equalTo(self.logImg.mas_right).offset(__I6Width(15));
            make.right.equalTo(self.bgView).offset(__I6Width(-15));
            make.centerY.equalTo(self.logImg);
        }];
        
    }
    
    return _moneyTf;
}

- (SHLabel *)balanceLabel{
    if (!_balanceLabel) {
        if (self.withdrawCashType == SHWithdrawCashD0) {
            _balanceLabel = [SHLabel labelWithText:[NSString stringWithFormat:@"可用余额 ¥ %.2f",  [self.withdrawal_balance floatValue]] font:13 textColor:[UIColor colorWithHexString:@"#c0c0c7"] textAlignment:NSTextAlignmentLeft];
        }else{
            _balanceLabel = [SHLabel labelWithText:[NSString stringWithFormat:@"可用余额 ¥ %.2f",[UserModel shareManager].balance]font:13 textColor:[UIColor colorWithHexString:@"#c0c0c7"] textAlignment:NSTextAlignmentLeft];
        }
        
        [_bgView addSubview:_balanceLabel];
        [_balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(__I6Height(13));
            make.left.mas_equalTo(self.bgView.mas_left).with.offset(__I6Width(15));
            make.bottom.mas_equalTo(self.bgView.mas_bottom).with.offset(__I6Width(-12));
            
            
        }];
    }
    return _balanceLabel;
}

- (SHButton *)withdrawCashAllMoney{
    if (!_withdrawCashAllMoney) {
        _withdrawCashAllMoney = [SHButton buttonWithType:UIButtonTypeCustom title:@"全部提现" font:13 titleColor:[UIColor colorWithHexString:@"ff801a"] textAlignment:UIControlContentHorizontalAlignmentLeft forState:UIControlStateNormal];
        [_withdrawCashAllMoney addTarget:self action:@selector(withdrawCashAllMoneyClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:_withdrawCashAllMoney];
        [_withdrawCashAllMoney mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(__I6Height(30));
            make.right.mas_equalTo(self.bgView.mas_right).with.offset(__I6Width(-15));
            make.centerY.mas_equalTo(self.balanceLabel.mas_centerY);
        }];
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor  = KLINECOLOR;
        [_bgView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
            make.left.mas_equalTo(self.bgView.mas_left).with.offset(__I6Width(15));
            make.right.mas_equalTo(self.bgView.mas_right).with.offset(__I6Width(-15));
            make.top.mas_equalTo(self.moneyTf.mas_bottom).offset(__I6Height(5));
        }];
    }
    
    return _withdrawCashAllMoney;
}


- (SHButton *)withdrawCashBtn{
    
    if (!_withdrawCashBtn) {
        _withdrawCashBtn = [SHButton buttonWithType:UIButtonTypeCustom title:@"确定" font:17 titleColor:[UIColor whiteColor] textAlignment:0 forState:UIControlStateNormal];
//        [_withdrawCashBtn setBackgroundImage:[UIImage imageNamed:@"sm_navigationbar"] forState:UIControlStateNormal];
        _withdrawCashBtn.backgroundColor = [UIColor colorWithHexString:@"2BA2D7"];

        [_withdrawCashBtn setCornerRadius:5 bounds:CGRectMake(0, 0, __I6Width(326), __I6Height(42))];
        [_withdrawCashBtn addTarget:self action:@selector(withdrawCashBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_withdrawCashBtn];
        [_withdrawCashBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(__I6Height(42));
            make.width.mas_equalTo(__I6Width(326));
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.top.mas_equalTo(self.bgView.mas_bottom).offset(__I6Height(50));
        }];
    }
    
    return _withdrawCashBtn;
}

#pragma mark - 选择银行卡
- (void)withdrawCashBankCard:(BankCardModel *)model{
    self.bankCardModel = model;
    self.bankCardCodeId = [NSString stringWithFormat:@"%@",model.bank_card_code];
    self.bankCardName = model.bank_name;
    self.banckCardNameLabel.text = self.bankCardName;
    
    NSString *banckCardCode = model.bank_card_code;
    if (banckCardCode.length > 4) {
        NSInteger lenght = banckCardCode.length;
        NSRange range = {lenght - 4,4};
        banckCardCode =  [banckCardCode substringWithRange:range];
        self.cardIdLabel.text= [NSString stringWithFormat:@"尾号%@",banckCardCode];
    }
}

#pragma mark - 获取默认银行卡
- (void)requestBankCardInfo{
    
    
    [SHUserCenterHttp getBankCardLsitWithUsingChoices:1 bankCardType:11 success:^(NSMutableArray *bankCardListArray) {
        
        for (BankCardModel *model in bankCardListArray) {
            
            if (model.default_use ) {
                self.bankCardModel = model;
                self.bankCardCodeId = [NSString stringWithFormat:@"%@",model.bank_card_code];
                self.bankCardName = model.bank_name;
                self.banckCardNameLabel.text = self.bankCardName;
                
                NSString *banckCardCode = model.bank_card_code;
                if (banckCardCode.length > 4) {
                    NSInteger lenght = banckCardCode.length;
                    NSRange range = {lenght - 4,4};
                    banckCardCode =  [banckCardCode substringWithRange:range];
                    self.cardIdLabel.text= [NSString stringWithFormat:@"尾号%@",banckCardCode];
                }
                break;
            }
        }
        
    } complection:^(id result) {
        
    } failure:^(id erro) {
        
    }];
}

- (void)withdrawCashAllMoneyClicked:(UIButton *)sender{
    if (self.withdrawCashType == SHWithdrawCashD0) {
        self.moneyTf.text =   [NSString stringWithFormat:@"%.2f",[self.withdrawal_balance floatValue]];
        
    }else{
        self.moneyTf.text = [NSString stringWithFormat:@"%.2f",[UserModel shareManager].balance];
    }
}
#pragma mark - 我的粮仓提现
- (void)withdrawCashBtnClicked:(UIButton *)sender{
    
    if (!self.moneyTf.text || ([self.moneyTf.text floatValue] <= 0)) {
        [self.view makeToast:@"请输入提现金额！" duration:2 position:@"center"];
        return;
    }
    
    if (([self.moneyTf.text floatValue] < 10) && (self.withdrawCashType != SHWithdrawCashTurnOutToD0)) {
        [self.view makeToast:@"提现金额必须大于10元！" duration:2 position:@"center"];
        return;
    }
    if ([self.moneyTf.text floatValue] > [self.withdrawal_balance floatValue]) {
        [self.view makeToast:@"提现金额超过可提现金额！" duration:2 position:@"center"];
        return;
    }
    if (!self.bankCardCodeId) {
        [self.view makeToast:@"卡号为空" duration:2 position:@"center"];
        return;
    }
    
    //查询是否设置过支付密码 -- 设置过弹出密码框 未设置 跳转密码设置页面
    if ([UserModel shareManager].has_pay_password) {//已经设置过支付密码，直接支付
        
        [[UIApplication sharedApplication].keyWindow addSubview:self.payPasswordView];
        [self.payPasswordView show];
        
    }else{//未设置支付密码
        
        SHUserPayPasswordController *payPassWordVc = [[SHUserPayPasswordController alloc]init];
        [self.navigationController pushViewController:payPassWordVc animated:YES];
    }
}
#pragma mark - SHPasswordViewDelegate
#pragma mark -- 获取支付密码
- (void)passwordView:(SHPayPasswordView *)passwordView passwordKey:(NSString *)passwordKey{
    
    SHLog(@"获取支付密码 %@",passwordKey);
    
    [MBProgressHUD showHUDAddedTo:passwordView.contenView  animated:YES];
    
    NSDictionary *params = @{@"pay_password": passwordKey};
    [MyDataService requestAFURL:KUSERCheck_Pay_PassWord httpMethod:@"POST" params:params data:nil complection:^(id result) {
        
        [MBProgressHUD hideHUDForView:passwordView.contenView animated:YES];
        
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            
            [self urserWithdraw:passwordView];
        }else{
            
            [passwordView showPayPasswordViewSubView];
            [passwordView makeToast:@"密码错误" duration:1.5 position:@"center"];
            [passwordView clearPassword];
            
            [MBProgressHUD hideHUDForView:passwordView.contenView animated:YES];
        }
    } failure:^(id erro) {
        
        [MBProgressHUD hideHUDForView:passwordView.contenView animated:YES];
        [self.view makeToast:@"拼命链接中，稍等哈" duration:1.5 position:@"center"];
    }];
}

#pragma mark - 我的粮仓提现
- (void)urserWithdraw:(SHPayPasswordView *)passwordView{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *params = @{@"amount":@([self.moneyTf.text floatValue]),@"accNo":self.bankCardCodeId};
    [MyDataService requestAFURL:KWithDraw  httpMethod:@"POST" params:params data:nil complection:^(id result) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            //隐藏支付完成动画
            [passwordView hidePayPasswordViewSubView];
            [XLPaymentSuccessHUD hideIn:passwordView.contenView];
            //显示支付中动画
            [XLPaymentLoadingHUD showIn:passwordView.contenView];
            
            //隐藏支付中成动画
            [XLPaymentLoadingHUD hideIn:passwordView.contenView];
            //显示支付完成动画
            [XLPaymentSuccessHUD showIn:passwordView.contenView];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                
                [passwordView dissmissMenuView];
                //刷新可提现金额
                [self requstBalance];
                
                [passwordView makeToast:result[@"data"] duration:1.5 position:@"center"];
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [passwordView showPayPasswordViewSubView];
            [passwordView makeToast:result[@"data"] duration:1.5 position:@"center"];
            [passwordView clearPassword];
            
            [MBProgressHUD hideHUDForView:passwordView.contenView animated:YES];
            
        }
    } failure:^(id erro) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
    
}

#pragma mark - 我的D0转出
- (void)d0Turnedout:(SHPayPasswordView *)passwordView{
    
    NSDictionary *params = @{@"amount":@([self.moneyTf.text floatValue]),@"bankcard_id":self.bankCardCodeId};
    [MyDataService requestAFURL:KTurnedout  httpMethod:@"POST" params:params data:nil complection:^(id result) {
        
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            
            //隐藏支付完成动画
            [passwordView hidePayPasswordViewSubView];
            [XLPaymentSuccessHUD hideIn:passwordView.contenView];
            //显示支付中动画
            [XLPaymentLoadingHUD showIn:passwordView.contenView];
            
            //隐藏支付中成动画
            [XLPaymentLoadingHUD hideIn:passwordView.contenView];
            //显示支付完成动画
            [XLPaymentSuccessHUD showIn:passwordView.contenView];
            
            
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                
                [passwordView dissmissMenuView];
                [self refreshUserBalanceLuckyInfo];
                
                [passwordView makeToast:result[@"data"] duration:1.5 position:@"center"];
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            
            [passwordView showPayPasswordViewSubView];
            [passwordView makeToast:result[@"data"] duration:1.5 position:@"center"];
            [passwordView clearPassword];
            
            [MBProgressHUD hideHUDForView:passwordView.contenView animated:YES];
        }
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
    
}


#pragma mark -- 获取刷新我的粮仓可提现余额
- (void)requstBalance{
    
    [MyDataService requestAFURL:KUSER_GET_CURRENCY httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            
            NSNumber *balance = result[@"data"][@"balance"];
            // NSNumber *deposit = result[@"data"][@"deposit"];
            self.withdrawal_balance = balance;
            [[UserModel shareManager] setBalance:[balance floatValue]];
            self.balanceLabel.text = [NSString stringWithFormat:@"可用余额 ¥ %.2f",  [balance floatValue]];
        }
        
    } failure:^(id erro) {
        
    }];
    
    
}

#pragma mark - 刷新D0粮仓可提现金额
- (void)refreshUserBalanceLuckyInfo{
    
    [SHBalanceModelView getUserBalanceLuckyInfoWithSuccess:^(UserBalanceModel *userBalanceModel) {
        if (userBalanceModel) {
            self.withdrawal_balance= userBalanceModel.withdrawal_balance;
            self.balanceLabel.text = [NSString stringWithFormat:@"可用余额 ¥ %.2f",  [userBalanceModel.withdrawal_balance floatValue]];
        }
    } complection:^(id result) {
        
    } failure:^(id erro) {
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.moneyTf resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark --- 文本框代理
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.moneyTf) {
        NSScanner      *scanner    = [NSScanner scannerWithString:string];
        NSCharacterSet *numbers;
        NSRange         pointRange = [textField.text rangeOfString:@"."];
        
        if ( (pointRange.length > 0) && (pointRange.location < range.location  || pointRange.location > range.location + range.length) )
        {
            numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        }
        else
        {
            numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
        }
        
        if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] )
        {
            return NO;
        }
        
        short remain = 2; //默认保留2位小数
        
        NSString *tempStr = [textField.text stringByAppendingString:string];
        NSUInteger strlen = [tempStr length];
        if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
            if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时，如果再输入“.”则被视为无效。
                return NO;
            }
            if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”，当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数，则视当次输入无效。
                return NO;
            }
        }
        
        NSRange zeroRange = [textField.text rangeOfString:@"0"];
        if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
            if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){// 当输入框只有一个字符并且字符为“0”时，再输入不为“0”或者“.”的字符时，则将此输入替换输入框的这唯一字符。
                textField.text = string;
                return NO;
            }else{
                if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时，并且没有“.”字符时，如果当此输入的字符为“0”，则视当此输入无效。
                    if([string isEqualToString:@"0"]){
                        return NO;
                    }
                }
            }
        }
        
        NSString *buffer;
        if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) )
        {
            return NO;
        }
        
    }
    return YES;
}

- (void)backToVc:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}



@end
